package com.fmsz.physicalheadline.application;

import android.app.Application;

import com.fmsz.physicalheadline.entity.news.DaoMaster;
import com.fmsz.physicalheadline.entity.news.DaoSession;
import com.fmsz.physicalheadline.interfaces.HttpService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import org.greenrobot.greendao.AbstractDaoMaster;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/18.
 */

public class MyApplication extends Application {

    private HttpService httpService;
    private Gson gson;
    public static final String DB_NAME = "sportHead_db";
    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
//        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
//                .detectAll()
//                .penaltyDialog()
//                .penaltyDeath()
//                .build()
//        );
//        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
//                .detectAll()
//                .penaltyLog()
//                .penaltyDeath()
//                .build()
//        );
        initDatabase();
        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
        gson = new GsonBuilder()
                .registerTypeAdapter(Date.class, new TypeAdapter<Date>() {
                    private final SimpleDateFormat SDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);

                    @Override
                    public void write(JsonWriter out, Date value) throws IOException {
                        if (value == null) {
                            out.nullValue();
                        } else {
                            out.value(SDF.format(value));
                        }
                    }

                    @Override
                    public Date read(JsonReader in) throws IOException {
                        if (in.hasNext()) {
                            String s = in.nextString();
                            try {
                                return SDF.parse(s);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }
                        return null;
                    }
                }).create();
        httpService = new Retrofit.Builder()
                .baseUrl("http://api.lifxapi.com/")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()
                .create(HttpService.class);
    }

    private void initDatabase() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, DB_NAME, null);
        DaoMaster master = new DaoMaster(devOpenHelper.getWritableDatabase());
        daoSession = master.newSession();
    }

    public HttpService getHttpService() {
        if (httpService == null) {
            httpService = new Retrofit.Builder()
                    .baseUrl("http://api.lifxapi.com/")
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build()
                    .create(HttpService.class);
        }
        return httpService;
    }
    public static DaoSession getDaoSessionInstace(){
        return daoSession;
    }

}
